<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	
	public function index()
	{
		$this->load->helper('url');
		
		$this->load->model('db_model');
		$this->db_model->loadTable('users');
		if($this->db_model->findCount() == 0)
		{
			redirect('user/add','refresh'); 
			
		}
		// auth
		$this->load->library('quickauth');
		if(!$this->quickauth->logged_in())
		{
			redirect('user/login','refresh');
		}
		redirect('admin', 'refresh');				
	}
	public function add()
	{
		$this->load->model('db_model');
		$this->db_model->loadTable('users');
		if($this->db_model->findCount() > 0)
		{
			redirect('user/login','refresh'); 
		}
		
		$this->load->helper(array('url', 'form'));
		$this->load->library(array('form_validation', 'session'));
		$this->load->model('admin_nav_model');
		$menu = $this->admin_nav_model->get_nav();
		
		$config['view'] = 'user_add_view';
		$config['menu'] = $menu;
		$this->form_validation->set_rules('username', '用户名', 'required');
		$this->form_validation->set_rules('password_confirm', '确认密码', 'trim|required');
		$this->form_validation->set_rules('password', '密码', 'trim|required|matches[password_confirm]');
		if ($this->form_validation->run() == false)
		{
			$this->load->view('global_view', $config);
		}
		else
		{
			$userdata = array(
			    'username' => $this->input->post('username'),
			    'password' => $this->input->post('password')
			);
			// auth
			$this->load->library('quickauth');	
			$this->quickauth->register($userdata);

			$this->session->set_flashdata('message', '添加用户成功');
			//$this->quickauth->login($this->input->post('username'), $this->input->post('password'));
			redirect('admin', 'refresh');
		}
		
	}
	
	public function login() 
	{
		$this->load->helper(array('url', 'form'));
		$this->load->library(array('form_validation', 'session'));
		
		$this->load->model('admin_nav_model');
		$menu = $this->admin_nav_model->get_nav();
		
		$config['view'] = 'user_login_view';
		$config['menu'] = $menu;
		
		$this->form_validation->set_rules('username', '用户名', 'required');
		$this->form_validation->set_rules('password', '密码', 'required');
		if ($this->form_validation->run() == false)
		{
			$this->load->view('global_view', $config);
		}
		else
		{
			// auth
			$this->load->library('quickauth');
			$this->quickauth->login($this->input->post('username'), $this->input->post('password'), $this->input->post('remember'));
			redirect('admin', 'refresh');
		}
		
		
	}
	function logout()
	{
		$this->load->helper('url');
		$this->load->library('quickauth');
		$this->quickauth->logout();
		redirect('user/login', 'refresh');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */